$(function() {  
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    window.face = '';
    window.back = '';
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    ajaxRequest('/Home/Order/getTotalCharity', {}, 'post', function(data) {
        $('.totalCharity').text(data.data.totalCharity);
    }, function(error) {
    });     
    //身份证
    var confirmationModal = $('#confirmationModal');
    var idCardChangeModal = $('#idCardChangeModal'); 
    var idCardModal = $('#idCardModal');
    var bindMobileModal = $('#bindMobileModal');
    ajaxRequest('/Home/User/getPersonIdInfo', {}, 'post', function(data) {
        var info = data.data;
        if (info.WechatAndBind) {
            $('.unBindIdCard').attr('data-isWechat',true);
        }else{
            $('.unBindIdCard').removeAttr('data-isWechat');
        }
        if(info.isBind){
            $('.bindIdCardNum').removeClass('hidden');
            $('.unBindIdCard').addClass('hidden');
            idCardModal.find('.idCard-name').text(info.idname);
            if(info.idsex == 1){
                idCardModal.find('.idCard-sex').text(lang('male'));
            }else{
                idCardModal.find('.idCard-sex').text(lang('female'));
            }
            idCardModal.find('.idCard-nation').text(info.idnation);
            idCardModal.find('.idCard-num').text(info.ididentity);
            idCardModal.find('.idCard-address').text(info.idaddress);  
            $('.bindIdCardNum').removeClass('hidden').text(info.ididentity.substr(0,4)+'****'+info.ididentity.substr(-4));
        }else{
            $('.unBindIdCard').removeClass('hidden');
            $('.bindIdCardNum').addClass('hidden');
        }
    }, function(error) {
        toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
    });    
    $('#doc-body').on('click','.idCard-one-icon',function(e){
        e.preventDefault();
        e.stopPropagation();
        if($(this).find('.unBindIdCard').hasClass('hidden')){
            $('#idCardModal').modal('show');
        }
    }).on('click','.unBindIdCard',function(e){
        e.preventDefault();
        e.stopPropagation();
        if($(this).attr('data-isWechat')){
            bindMobileModal.modal('show');
        }else{
            idCardChangeModal.modal('show');
        }
        $('.get-photo').removeClass('hidden');
        $('.add-photo').addClass('hidden').html(''); 
    });  
    $('#submitEditIdCard').on('click',function(e){
        if(window.face == ''){
            toastr.error(lang('select.pic'));
            return false;
        }
        e.preventDefault();
        e.stopPropagation();
        toggleLoading(true); 
        Parmas = {};
        Parmas.baseface = window.face;
        if(window.back != ''){
            Parmas.baseback = window.back;
        }
        ajaxRequest('/Home/User/getIdInformation', Parmas, 'post',function(data){
            var info = data.data;
            toggleLoading(false);
            $('#submitConfirmation').data('info',info);
            confirmationModal.find('.idCard-name').text(info.name);
            confirmationModal.find('.idCard-sex').text(info.sex);
            confirmationModal.find('.idCard-nation').text(info.nationality);
            confirmationModal.find('.idCard-num').text(info.num);
            confirmationModal.find('.idCard-address').text(info.address);
            idCardChangeModal.modal('hide');
            confirmationModal.modal('show');
        }, function(error) {
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        }); 
    });
    
    //绑定手机号
    $('#bindMobileModal').inputValidate({
        submit: {
            query: '#sendAgain'
        },
        inputs: [{
            query: '#requestMobile',
            regexp: window.commonRegexp.mobile.regexp,
            message: window.commonRegexp.mobile.message,
            emptyMessage: window.commonRegexp.mobile.emptyMessage
        }]
    });
    $('#sendAgain').on('successSubmit.amos.inputValidate', function (e) {
        var submitBtn = $(this);
        var params = {};
        params.mobile = $('#requestMobile').val();
        params.areaCode = $('.areaCodeValue').text().trim();
        toggleLoading(true);
        ajaxRequest('/Home/User/wechatBookVcode', params, 'post', function (data) {
            toggleLoading(false);
            timeDisplay($('#sendAgain'), 60);
        }, function (error, obj, defaultErrorHandler) {
            toggleLoading(false);
            defaultErrorHandler(error, obj);
        }, submitBtn);
    });
    
    $('#login-container').inputValidate({
        submit: {
            query: '#requestSubmit'
        },
        inputs: [{
            query: '#requestMobile',
            regexp: window.commonRegexp.mobile.regexp,
            message: window.commonRegexp.mobile.message,
            emptyMessage: window.commonRegexp.mobile.emptyMessage
        }, {
            query: '#requestMobileCode',
            regexp: window.commonRegexp.vcode.regexp,
            message: window.commonRegexp.vcode.message,
            emptyMessage: window.commonRegexp.vcode.emptyMessage
        }]
    });
    $('#requestSubmit').on('successSubmit.amos.inputValidate', function (e) {
        var submitBtn = $(this);
        var params = {};
        params.mobile = $('#requestMobile').val();
        params.vcode = $('#requestMobileCode').val();
        params.areaCode = $('.areaCodeValue').text().trim();
        params.hotel = currentHotel;
        toggleLoading(true);
        ajaxRequest('/Home/User/wechatBindBook', params, 'post',function(data){
            var info = data.data;
            $('.unBindIdCard').removeAttr('data-isWechat');
            bindMobileModal.modal('hide');
            toggleLoading(false);
            if(info.isBind){
                $('.bindIdCardNum').removeClass('hidden');
                $('.unBindIdCard').addClass('hidden');
                idCardModal.find('.idCard-name').text(info.idname);
                if(info.idsex == 1){
                    idCardModal.find('.idCard-sex').text(lang('male'));
                }else{
                    idCardModal.find('.idCard-sex').text(lang('female'));
                }
                idCardModal.find('.idCard-nation').text(info.idnation);
                idCardModal.find('.idCard-num').text(info.ididentity);
                idCardModal.find('.idCard-address').text(info.idaddress);  
                $('.user-num').text('+'+info.areacode+' '+info.mobile);
                $('.bindIdCardNum').removeClass('hidden').text(info.ididentity.substr(0,4)+'****'+info.ididentity.substr(-4));
                $('#idCardModal').modal('show');
            }else{
                $('.unBindIdCard').removeClass('hidden');
                $('.bindIdCardNum').addClass('hidden');
                idCardChangeModal.modal('show');
            }            
        }, function (error, obj, defaultErrorHandler) {
            toggleLoading(false);
            defaultErrorHandler(error, obj);
        }, submitBtn);
    });
    $('#bindMobileModal').bind('input propertychange',function(){
        var mobile = $('#requestMobile').val();
        var vcode = $('#requestMobileCode').val();
        if(mobile && vcode){
            $('#requestSubmit').attr('disabled', false);          
        }else{
            $('#requestSubmit').attr('disabled', true);           
        }

    });    
    
    $('#submitConfirmation').on('click',function(e){
        e.preventDefault();
        e.stopPropagation();
        var info =$(this).data('info');
        if(info.sex == '男'){
            var sex = 1;
        }else{
            var sex = 0;
        }
        var param = {};
        param.idName = info.name;
        param.idSex = sex;
        param.idNation = info.nationality;
        param.idIdentity = info.num;
        param.idAddress = info.address;
        ajaxRequest('/Home/User/saveInformation', param, 'post',null, function(error) {
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });            
    });
    $('#idCardModal').on('click','.header-reset',function(){
        $('#idCardModal').modal('hide');
        idCardChangeModal.modal('show');
    });
    idCardChangeModal.on('click','.header-cancel', function (e) {
        e.preventDefault();
        e.stopPropagation();
        idCardChangeModal.modal('hide');
        $('.get-photo').removeClass('hidden');
        $('.add-photo').addClass('hidden').html(''); 
        $("input[type='file']").val('');
    })
    confirmationModal.on('click','.confirmation-cancel', function (e) {
        e.preventDefault();
        e.stopPropagation();
        confirmationModal.modal('hide');
        $('.get-photo').removeClass('hidden');
        $('.add-photo').addClass('hidden').html(''); 
        $("input[type='file']").val('');
    }).on('click','.confirmation-back', function (e) {
        e.preventDefault();
        e.stopPropagation();
        confirmationModal.modal('hide');
        idCardChangeModal.modal('show');
    });     
    $("input[type='file']").on('change', function (e) {
        var self = $(this);  
        var file = e.target.files[0];
        drawOnCanvas(file,self);   
    }); 
    function drawOnCanvas(file,self) {
        var reader = new FileReader();
        var cameraContainer = self.closest('.camera-container');
        var value = cameraContainer.data('value');
        var width = cameraContainer.width();
        var height = cameraContainer.height();
        reader.onload = function(e) {
            var dataURL = e.target.result,
            canvas = document.querySelector('canvas'),
            ctx = canvas.getContext('2d'),
            img = new Image();
            img.onload = function() {
                var square = 320;
                canvas.width = square;
                canvas.height = square;
                var context = canvas.getContext('2d');
                context.clearRect(0, 0, square, square);
                var imageWidth;
                var imageHeight;
                var offsetX = 0;
                var offsetY = 0;
                if (this.width > this.height) {
                    imageWidth = Math.round(square * this.width / this.height);
                    imageHeight = square;
                    offsetX = -Math.round((imageWidth - square) / 2);
                } else {
                    imageHeight = Math.round(square * this.height / this.width);
                    imageWidth = square;
                    offsetY = -Math.round((imageHeight - square) / 2);
                }
                context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);
                var base64 = canvas.toDataURL('image/jpeg', 0.7);
                if(parseInt(value) == 1){
                    window.face = base64; 
                }else{
                    window.back = base64;
                }
                cameraContainer.find('.get-photo').addClass('hidden');
                cameraContainer.find('.add-photo').removeClass('hidden').append(img);
                cameraContainer.find('img').css({'width':width,'height':height});
            };
            img.src = dataURL;
        };
        reader.readAsDataURL(file);
    }
}); 
